﻿using System.Net;
using System.Globalization;
using System.Text;
using System.Runtime.InteropServices;
namespace System.Web
{
    public abstract class NetHttpWorkerRequest : HttpListenerWorkerRequest
    {
        public delegate void EndOfSendNotification(NetHttpWorkerRequest wr, object extraData);

        public NetHttpWorkerRequest(HttpListenerContext context)
            : base(context) { }

        internal void SetRemapHandler(string handlerType, string handlerName)
        {
        }

        public virtual void CloseConnection() { }

        internal abstract void EndOfRequest();

        public abstract void FlushResponse(bool finalFlush);
        public virtual string GetAppPath() { return null; }
        public virtual string GetAppPathTranslated() { return null; }

        //public virtual long GetBytesRead() { return 0L; }

        public virtual string GetKnownRequestHeader(int index) { return null; }

        public virtual string GetServerVariable(string name) { return null; }

        public virtual int GetTotalEntityBodyLength()
        {
            var knownRequestHeader = GetKnownRequestHeader(11);
            int num = 0;
            if (knownRequestHeader != null)
                try { num = int.Parse(knownRequestHeader, CultureInfo.InvariantCulture); }
                catch { }
            return num;
        }

        public virtual string GetUnknownRequestHeader(string name) { return null; }

        public virtual string[][] GetUnknownRequestHeaders() { return null; }

        public virtual bool HeadersSent() { return true; }

        public virtual int ReadEntityBody(byte[] buffer, int size) { return 0; }

        public virtual int ReadEntityBody(byte[] buffer, int offset, int size)
        {
            var buffer2 = new byte[size];
            int count = ReadEntityBody(buffer2, size);
            if (count > 0)
                Buffer.BlockCopy(buffer2, 0, buffer, offset, count);
            return count;
        }

        public virtual void SendCalculatedContentLength(int contentLength) { }

        public virtual void SendCalculatedContentLength(long contentLength) { SendCalculatedContentLength(Convert.ToInt32(contentLength)); }

        public abstract void SendKnownResponseHeader(int index, string value);
        public abstract void SendResponseFromFile(IntPtr handle, long offset, long length);
        public abstract void SendResponseFromFile(string filename, long offset, long length);

        public virtual void SendResponseFromMemory(IntPtr data, int length)
        {
            if (length > 0)
            {
                var dest = new byte[length];
                Marshal.Copy(new IntPtr(data.ToInt64()), dest, 0, length);
                SendResponseFromMemory(dest, length);
            }
        }

        public abstract void SendResponseFromMemory(byte[] data, int length);
        internal virtual void SendResponseFromMemory(IntPtr data, int length, bool isBufferFromUnmanagedPool) { SendResponseFromMemory(data, length); }

        public abstract void SendStatus(int statusCode, string statusDescription);
        internal virtual void SendStatus(int statusCode, int subStatusCode, string statusDescription) { SendStatus(statusCode, statusDescription); }

        public abstract void SendUnknownResponseHeader(string name, string value);
        public virtual void SetEndOfSendNotification(EndOfSendNotification callback, object extraData) { }

        internal virtual void SetHeaderEncoding(Encoding encoding) { }

        internal virtual void TransmitFile(string filename, long length, bool isImpersonating) { TransmitFile(filename, 0L, length, isImpersonating); }
        internal virtual void TransmitFile(string filename, long offset, long length, bool isImpersonating) { SendResponseFromFile(filename, offset, length); }
    }
}
